package com.ljx.itbbs.server.article.controller;

import com.ljx.itbbs.common.domain.vo.resp.ApiResult;
import com.ljx.itbbs.common.domain.vo.resp.CursorResp;
import com.ljx.itbbs.common.utils.SecurityUtil;
import com.ljx.itbbs.common.utils.ServletUtil;
import com.ljx.itbbs.server.article.domain.vo.req.ArticleCommentReq;
import com.ljx.itbbs.server.article.domain.vo.req.CommentSaveReq;
import com.ljx.itbbs.server.article.domain.vo.resp.CommentReplyResp;
import com.ljx.itbbs.server.article.domain.vo.resp.CommentResp;
import com.ljx.itbbs.server.article.service.CommentReplyService;
import com.ljx.itbbs.server.article.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "评论接口")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private CommentReplyService commentReplyService;

    @ApiOperation("发表评论")
    @PostMapping("/post")
    public ApiResult<Void> postComment(@Valid @RequestBody CommentSaveReq req){
        commentService.postComment(ServletUtil.getUserId(), req);

        return ApiResult.success();
    }

    @ApiOperation("查询评论回复")
    @GetMapping("/reply")
    public ApiResult<CursorResp<CommentReplyResp>> getCommentReply(@Valid ArticleCommentReq req){

        return ApiResult.success(commentReplyService.reply(req));
    }

    @ApiOperation("最新评论")
    @GetMapping("/new")
    public ApiResult<List<CommentResp>> getNewComment(){

        return ApiResult.success(commentService.getNewComment());
    }

    @ApiOperation("点赞")
    @PostMapping("/like/{type}/{commentId}")
    public ApiResult<Void> likeComment(@PathVariable("commentId") Long commentId, @PathVariable("type") Integer type){
        commentService.likeComment(ServletUtil.getUserId(), commentId, type);

        return ApiResult.success();
    }
}
